***Non-Pecuniary Benefits***

/*File decomposes BP measure and generates indices using predictors from non-time-varying
and time-varying characteristics of the local area. Using these indices then regresses transaction
prices. Generates Table 7, and Internet Appendix Table D.8 and D.9

Requires following files:
1) QOL_Predictors_BP.dta
2) Regression_Analysis_pseudo.dta

*Note that the transaction variables from Regression_Analysis_pseudo.dta are randomized
and only a subset of the total data is provided due to proprietary subscription data from Pratt Stats.
*/


**********************************************************************************
****THIS IS THE REGRESSION FOR THE FIRST STAGE OF THE PREDICTORS OF BEST PLACES***
**********************************************************************************
		use "${Data}\QOL_Predictors_BP.dta", clear
		***THIS BUILDS INDICES FOR BOTH USING LPM***
		gen l_Sunlight = ln(cbsa_Mean_Jan_Sunlight)
		replace l_pop_1900 = ln(1+pop_1900)
		replace l_Precipitation =  ln(1+Precipitation_clean)
		replace l_Greater_90 = ln(1+Greater_90_clean)
		replace l_Less_20 = ln(1+Less_20_clean)
		gen l_Humidity = ln(1+cbsa_Mean_July_Humidity)
		replace l_median_house = ln(median_house_value_CBSA_imputed)
		replace l_pop_cbsa = ln(pop_cbsa)
		replace l_pop_density = ln(pop_density)
		replace l_cbsa_tax = ln(cbsa_tax)
		replace l_pcpi = ln(pcpi)
		rename cbsa_l_Percent_Water l_Water 
		rename cbsa_Topography_Code Topography_Code
		rename cbsa_percent_households_200k cbsa_200k 
		rename cbsa_bachelors_or_higher cbsa_bachelors_high

		*Declare Variables
		global BPNonTime l_pop_1900 l_Precipitation l_Greater_90 l_Less_20 l_Sunlight l_Humidity l_Water Topography_Code
		global BPTime Percent_UnHealthy_Air Student_to_Teacher property_crime_rate violent_crime_rate recreation_estab_pc eating_estab_pc drinking_estab_pc

		*Baseline Regression
		reghdfe BP_CBSA $BPNonTime $BPTime /// 
		BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax l_median_house l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa transactions_cbsa_avg_sc local_industry_conc, absorb(year) cluster(cbsa)
		gen sample1=e(sample)
		
		*Generate individual indices
		gen BP_index_nontime=.
		local count=1
		foreach x of global BPNonTime {
			gen b_`x'=_b[`x']
			gen BP_indiv`x'=b_`x'*`x'
			if `count'==1{
				replace BP_index_nontime=BP_indiv`x' if sample1==1 & ~missing(`x') & missing(BP_index_nontime)
			}
			else{
				replace BP_index_nontime=BP_index_nontime+BP_indiv`x' if sample1==1 & ~missing(`x') & ~missing(BP_index_nontime)
			}
			local count=`count'+1
			}
		gen BP_index_time=.
		local count=1
		foreach x of global BPTime {
			gen b_`x'=_b[`x']
			gen BP_indiv`x'=b_`x'*`x'
			if `count'==1{
				replace BP_index_time=BP_indiv`x' if sample1==1 & ~missing(`x') & missing(BP_index_time)
			}
			else{
				replace BP_index_time=BP_index_time+BP_indiv`x' if sample1==1 & ~missing(`x') & ~missing(BP_index_time)
			}
			local count=`count'+1
			}
	**PCA Analysis
		pca l_pop_1900 l_Precipitation l_Greater_90 l_Less_20 l_Sunlight l_Humidity l_Water Topography_Code if sample1==1
		esttab using "${Tables}\PCA_Decomp_non-time.tex", booktabs /// 
			cells("L[1](t label(Component 1)) L[2](t label(Component 2)) L[3](t label(Component 3)) L[4](t label(Component 4))") nogap noobs nonumber nomtitle replace
		predict pcNonTime1 pcNonTime2 pcNonTime3 pcNonTime4
		pca  Percent_UnHealthy_Air Student_to_Teacher property_crime_rate violent_crime_rate recreation_estab_pc eating_estab_pc drinking_estab_pc if sample1==1
		esttab using "${Tables}\PCA_Decomp_time.tex", booktabs /// 
			cells("L[1](t label(Component 1)) L[2](t label(Component 2)) L[3](t label(Component 3)) L[4](t label(Component 4))") nogap noobs nonumber nomtitle replace
		predict pcTime1 pcTime2 pcTime3 pcTime4
		
		
			
		reghdfe BP_CBSA pcNonTime* pcTime* ///
			BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax l_median_house ///
			l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa transactions_cbsa_avg_sc local_industry_conc, absorb(year) cluster(cbsa)
		gen sample2=e(sample)
		
		*Generate individual indices for PCA
		gen BP_index_non_PCA=.
		local count=1
		foreach x of var pcNonTime* {
			gen b_`x'=_b[`x']
			gen BP_indiv`x'=b_`x'*`x'
			if `count'==1{
				replace BP_index_non_PCA=BP_indiv`x' if sample1==1 & ~missing(`x') & missing(BP_index_non_PCA)
			}
			else{
				replace BP_index_non_PCA=BP_index_non_PCA+BP_indiv`x' if sample2==1 & ~missing(`x') & ~missing(BP_index_non_PCA)
			}
			local count=`count'+1
			}
			
		gen BP_index_time_PCA=.
		local count=1
		foreach x of var pcTime* {
			gen b_`x'=_b[`x']
			gen BP_indiv`x'=b_`x'*`x'
			if `count'==1{
				replace BP_index_time_PCA=BP_indiv`x' if sample1==1 & ~missing(`x') & missing(BP_index_time_PCA)
			}
			else{
				replace BP_index_time_PCA=BP_index_time_PCA+BP_indiv`x' if sample2==1 & ~missing(`x') & ~missing(BP_index_time_PCA)
			}
			local count=`count'+1
			}
		
		*Tables for Top-10	
		preserve
		collapse (mean) BP_index_nontime BP_index_time, by(cbsa cbsaname)
		keep cbsa cbsaname BP_index_nontime BP_index_time
		gsort -BP_index_nontime
		keep if _n<=10
		export excel using "${Tables}\BP_top10_non-time-varying.xlsx", firstrow(variables) replace
		restore
		preserve
		collapse (mean) BP_index_nontime BP_index_time, by(cbsa cbsaname)
		keep cbsa cbsaname BP_index_nontime BP_index_time
		gsort -BP_index_time
		keep if _n<=10
		export excel using "${Tables}\BP_top10_time-varying.xlsx", firstrow(variables) replace
		restore
		
			
			
			
		************************************************************************
		********************   Build Tables   **********************************
		************************************************************************
		label var l_pop_1900 "Log(Population in 1900)"
		label var l_Precipitation  "Log(Precipitation)"
		label var l_Greater_90 "Log(Number of Days > 90 F)"
		label var l_Less_20  "Log(Number of Days <20 F)"
		label var l_Sunlight "Log(Average January Sun)"
		label var l_Humidity "Log(Average July Humidity)"
		label var l_Water  "Log(\% of area covered by water)"
		label var Topography_Code "Topographical Variation"
		label var Percent_UnHealthy_Air "Percent of Days with Unhealthy Air"
		label var Student_to_Teacher "Student-Teacher Ratio"
		label var property_crime_rate "Property Crimes per 100K"
		label var violent_crime_rate  "Violent Crimes per 100K"
		label var recreation_estab_pc "Recreation Establishments per 1,000"
		label var eating_estab_pc "Eating Establishments per 1,000"
		label var drinking_estab_pc "Drinking Establishments per 1,000"
		
		
		est clear
		eststo exog: reghdfe BP_CBSA $BPNonTime /// 
		BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax /// 
		l_median_house l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa /// 
		transactions_cbsa_avg_sc local_industry_conc, absorb(year) cluster(cbsa)
			estadd local fin "No"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "Yes"
			estadd local industry "No"
			estadd local state "No"
		test $BPNonTime
			estadd scalar ftest = e(p) : exog
		eststo endog: reghdfe BP_CBSA $BPTime /// 
		BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax /// 
		l_median_house l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa /// 
		transactions_cbsa_avg_sc local_industry_conc, absorb(year) cluster(cbsa)
			estadd local fin "No"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "Yes"
			estadd local industry "No"
			estadd local state "No"
		test $BPTime
			estadd scalar ftest = e(p) : endog
		eststo both: reghdfe BP_CBSA $BPNonTime $BPTime /// 
		BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax /// 
		l_median_house l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa /// 
		transactions_cbsa_avg_sc local_industry_conc, absorb(year) cluster(cbsa)
			estadd local fin "No"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "Yes"
			estadd local industry "No"
			estadd local state "No"
		test $BPNonTime $BPTime
			estadd scalar ftest = e(p) :both
			
		esttab exog endog both using  "${Tables}\First_Stage_Decomp.tex", booktabs ///
		replace b(%12.3f) ar2(%8.2f) se(%8.3f)  star(* 0.1 ** 0.05 *** 0.01) /// 
		label eqlabels(none) alignment(c c) mlabels(none) collabels(none) /*collabels("(1)" "(2)" "(3)", lhs(`:var lab `e(depvar)'')) nonumbers*/  interaction($~\times~$) noconstant ///
		cells("b(fmt(3)star)" "se(fmt(3)par)")  /// addnote("t-statistics in parentheses; \sym{*} \$p<0.10\$, \sym{**} \$p<0.05\$, \sym{***} \$p<0.01\$")
		stats(geo additional yearFE ftest N r2_a, fmt(0 0 0 3 0 3) ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Geographic Controls"' `"Additional Controls"' `"Year Fixed Effects"' `"F-Test"' `"Observations"' `"Adjusted \(R^2\)"')) ///
		keep($BPNonTime $BPTime)
		
		keep $BPNonTime $BPTime BP_index_nontime BP_index_time /// 
		BM_cbsa cbsa_200k cbsa_bachelors_high l_pop_cbsa l_pop_density l_cbsa_tax /// 
		l_median_house l_pcpi d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa /// 
		transactions_cbsa_avg_sc local_industry_conc ///
		BP_index_non_PCA BP_index_time_PCA pcTime* pcNonTime* ///
		cbsa year
		save "${Data}\BP_Decomposition_cbsa_year.dta", replace
		
				
		***************************************************************************
		***THIS RUNS REGRESSION FOR THE SECOND PART OF TABLE ON AMENITY INDICES****
		***************************************************************************
		mmerge cbsa year using "${Data}\Regression_Analysis_pseudo.dta"
		keep if _merge==3
		label var BP_index_nontime "Index Non-Time Varying"
		label var BP_index_time  "Index Time Varying"
		global fin_controls l_Sales OPtoS EmployAgr AssumedLease
		global local_controls cbsa_bachelors_or_higher l_pop_cbsa l_pop_density l_cbsa_tax l_median_house l_pcpi  ///
		d5_pop_cbsa d5_pcpi_cbsa d5_employment d5_med_house_val_cbsa
		global channels cbsa_percent_households_200k transactions_cbsa_avg_sc local_industry_conc	
		
		egen BP_index_time_std=std( BP_index_time)
		egen BP_index_nontime_std=std( BP_index_nontime)
		label var BP_index_nontime_std "Index Non-Time Varying"
		label var BP_index_time_std  "Index Time Varying"
		***Non-time varying***
		*est clear
		eststo secondexog: reghdfe l_Price $fin_controls BP_index_nontime_std BM_cbsa $channels $local_controls, absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
			
		***Time-Varying***
		eststo secondendog: reghdfe l_Price $fin_controls BP_index_time_std BM_cbsa $channels $local_controls , absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
			
		***Both***
		eststo secondboth: reghdfe l_Price BP_index_nontime_std BP_index_time_std BM_cbsa $fin_controls $channels $local_controls, absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
			
		esttab secondexog secondendog secondboth using  "${Tables}\Second_Stage_Decomp.tex", booktabs ///
		replace b(%12.3f) ar2(%8.2f) se(%8.3f)  star(* 0.1 ** 0.05 *** 0.01) /// 
		label eqlabels(none) alignment(c c) mlabels(none) collabels(none) /*collabels("(1)" "(2)" "(3)", lhs(`:var lab `e(depvar)'')) nonumbers*/  interaction($~\times~$) noconstant ///
		cells("b(fmt(3)star)" "se(fmt(3)par)")  /// addnote("t-statistics in parentheses; \sym{*} \$p<0.10\$, \sym{**} \$p<0.05\$, \sym{***} \$p<0.01\$")
		stats(fin geo additional yearFE industry state N r2_a, fmt(0 0 0 0 0 0 0 3) ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Financial Controls"' `"Geographic Controls"' `"Additional Controls"' `"Year FE"' `"Industry x Year FE"' `"State FE"' `"Observations"' `"Adjusted \(R^2\)"')) ///
		keep(BP_index_nontime_std BP_index_time_std )
		
		esttab exog endog both secondexog secondendog secondboth using  "${Tables}\Both_Stage_Decomp.tex", booktabs ///
		replace b(%12.3f) ar2(%8.2f) se(%8.3f)  star(* 0.1 ** 0.05 *** 0.01) /// 
		label eqlabels(none) alignment(c c) mlabels(none) collabels(none) /*collabels("(1)" "(2)" "(3)", lhs(`:var lab `e(depvar)'')) nonumbers*/  interaction($~\times~$) noconstant ///
		mgroups("Best Place (=1)"  "Log(Price)", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		cells("b(fmt(3)star)" "se(fmt(3)par)")  /// addnote("t-statistics in parentheses; \sym{*} \$p<0.10\$, \sym{**} \$p<0.05\$, \sym{***} \$p<0.01\$")
		stats(geo additional fin yearFE industry state N r2_a, fmt(0 0 0 0 0 0 0 3) ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Geographic Controls"' `"Additional Controls"' `"Financial Controls"' `"Year FE"' `"Industry x Year FE"' `"State FE"' `"Observations"' `"Adjusted \(R^2\)"')) ///
		keep($BPNonTime $BPTime BP_index_nontime_std BP_index_time_std )
		
		eststo secondPCAexog: reghdfe l_Price   BP_index_non_PCA  BM_cbsa $fin_controls $channels $local_controls, absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
		eststo secondPCAendog: reghdfe l_Price   BP_index_time_PCA  BM_cbsa $fin_controls $channels $local_controls, absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
		eststo secondPCAboth: reghdfe l_Price   BP_index_non_PCA  BP_index_time_PCA BM_cbsa $fin_controls $channels $local_controls, absorb(ind_code_48##year state_id) cluster(cbsa)
			estadd local fin "Yes"
			estadd local geo "Yes" 
			estadd local additional "Yes" 
			estadd local yearFE "No"
			estadd local industry "Yes"
			estadd local state "Yes"
			
		esttab secondPCAexog secondPCAendog secondPCAboth using  "${Tables}\Second_Stage_Decomp_PCA.tex", booktabs ///
		replace b(%12.3f) ar2(%8.2f) se(%8.3f)  star(* 0.1 ** 0.05 *** 0.01) /// 
		label eqlabels(none) alignment(c c) mlabels(none) collabels(none) /*collabels("(1)" "(2)" "(3)", lhs(`:var lab `e(depvar)'')) nonumbers*/  interaction($~\times~$) noconstant ///
		mgroups("Log(Price)", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		cells("b(fmt(3)star)" "se(fmt(3)par)")  /// addnote("t-statistics in parentheses; \sym{*} \$p<0.10\$, \sym{**} \$p<0.05\$, \sym{***} \$p<0.01\$")
		stats(geo additional fin yearFE industry state N r2_a, fmt(0 0 0 0 0 0 0 3) ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Geographic Controls"' `"Additional Controls"' `"Financial Controls"' `"Year FE"' `"Industry x Year FE"' `"State FE"' `"Observations"' `"Adjusted \(R^2\)"')) ///
		keep(BP_index_non_PCA  BP_index_time_PCA)
		
		
		
		
		
		